package 线程.Lock锁;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class MyTh extends Thread{
    //定义一个静态变量所有的数据都用这个数据
    static int ticket=0;
    //创建Lock锁对象
    static Lock lock=new ReentrantLock();

    @Override
    public void run() {
        //1.循环
        while (true){
            //2.同步代码块
            lock.lock();
            try {
                if (ticket==100){
                break;
            }else {
                Thread.sleep(10);
                ticket++;
                System.out.println(getName() + "，卖第" + ticket + "张票");
                }
            } catch (InterruptedException e) {
                    throw new RuntimeException(e);
            }finally {
                lock.unlock();
            }
        }
    }
}
